package swd.deploy.frame_maintain;

import com.pub.database.*;
import java.util.*;

public class Business {
   QueryBean gqbnDAO=null;
   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }
   String selectTableDataToFld(View view) throws Exception {
      String lstrSQL=null;
      Hashtable<String,Object> lhtTableData=null;
      try{
         lstrSQL="select frame_no,sys_group,frame_name from wkf_frame where frame_no='" + view.getframe_no() + "'";
         lhtTableData=gqbnDAO.readSingleRowData(lstrSQL);
         if(lhtTableData==null){
            return "查無資料";
         }
         view.setframe_no(String.valueOf((lhtTableData.get("FRAME_NO").toString())));
         view.setsys_group(String.valueOf((lhtTableData.get("SYS_GROUP").toString())));
         view.setframe_name(String.valueOf((lhtTableData.get("FRAME_NAME").toString())));
         return "";
      }
      catch(Exception e){
         throw new Exception(e.toString());
      }
      finally{
         lhtTableData=null;
         //lhtTableData.clear();

      }
   }
   String exeUpdSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lvtrParam.add(view.getframe_no()==null ? null :String.valueOf(view.getframe_no()));
         lvtrParam.add(view.getsys_group()==null ? null :String.valueOf(view.getsys_group()));
         lvtrParam.add(view.getframe_name()==null ? null :String.valueOf(view.getframe_name()));
         lstrSQL="update wkf_frame set frame_no=?,sys_group=?,frame_name=? where frame_no='" + view.getframe_no() + "'";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){
            return "wkf_frame";
         }

         saveDataToWkf_Sys_Table(view.getframe_no());
         return "OK!";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e.toString());
      }
      finally{
         lvtrParam.clear();
      }
   }
   String exeInsSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lvtrParam.add((view.getframe_no()==null ? null : view.getframe_no()));
         lvtrParam.add((view.getframe_name()==null ? null : view.getframe_name()));
         lvtrParam.add((view.getsys_group()==null ? null : view.getsys_group()));
         lstrSQL="insert into wkf_frame(frame_no,frame_name,sys_group) values (?,?,?)";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){;
            return "wkf_frame";
         }
         lvtrParam.clear();
         saveDataToWkf_Sys_Table(view.getframe_no());

         return "";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e);
      }
      finally{
         lvtrParam.clear();
      }
   }

   void setDataViewForm(DataViewForm DataViewForm,View view,String pstrLoginUser) throws Exception{

       //StringBuffer stbCondition = new StringBuffer();
       StringBuffer stbPageActionURI = new StringBuffer("frame_maintain.jsp?rowsn=");
       //rowSn

       DataViewForm.setFieldsList(" seqno,sys_no,type_id,type_name,type_up");
       DataViewForm.setTablesList(" wkf_menu ");
       DataViewForm.setSQLCondition("sys_no in (" + getFrameSysNo(view) + ") and flag='1' ");
       DataViewForm.setOrderByFields("seqno");
       DataViewForm.setOrderBySeq("asc");
       DataViewForm.setGroupBy(null);
       DataViewForm.setHaving(null);

       String strColRelation[][] = new String[6][];

       strColRelation[0] = new String[5];
       strColRelation[1] = new String[8];
       strColRelation[2] = new String[5];
       strColRelation[3] = new String[5];
       strColRelation[4] = new String[5];
       strColRelation[5] = new String[5];

       strColRelation[0][0]="'目錄順序'";
       strColRelation[0][1]="INPUT";
       strColRelation[0][2]="WIDTH='8%'";
       strColRelation[0][3]="<INPUT TYPE='text' size='8' name='seqno' CLASS='input' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[0][4]="0";

       strColRelation[1][0]="'系統編號'";
       strColRelation[1][1]="LINK";
       strColRelation[1][2]=" nowrap WIDTH='10%' ";
       strColRelation[1][3]="<a target='_blank' href=";
       strColRelation[1][4]="1";
       strColRelation[1][5]="'menu_tree_maintain.jsp'";
       strColRelation[1][6]="sys_no=";
       strColRelation[1][7]="1";

       strColRelation[2][0]="'程式編號'";
       strColRelation[2][1]="INPUT";
       strColRelation[2][2]="WIDTH='10%' align='center'";
       strColRelation[2][3]="<INPUT TYPE='text' size='10' name='type_id' readonly CLASS='input_noborder' VALUE=";
       strColRelation[2][4]="2";

       strColRelation[3][0]="'主目錄名稱'";
       strColRelation[3][1]="INPUT";
       strColRelation[3][2]="WIDTH='67%' align='center'";
       strColRelation[3][3]="<INPUT TYPE='text' size='40' name='type_name' readonly CLASS='input_noborder' VALUE=";
       strColRelation[3][4]="3";

       strColRelation[4][0]="HIDDEN";
       strColRelation[4][1]="INPUT";
       strColRelation[4][2]="WIDTH='0%' ";
       strColRelation[4][3]="<INPUT TYPE='hidden' name='type_up' VALUE=";
       strColRelation[4][4]="4";

       strColRelation[5][0]="HIDDEN";
       strColRelation[5][1]="INPUT";
       strColRelation[5][2]="WIDTH='0%'";
       strColRelation[5][3]="<INPUT TYPE='HIDDEN' name='sys_no' VALUE=";
       strColRelation[5][4]="1";


       DataViewForm.setColRelation(strColRelation);//將欄位相關設定殖入ActionForm
       DataViewForm.setJumpPageButtonShow(false);   //是否要秀出上下頁按鈕
       DataViewForm.setPageBtnOnTop(false);         //將button and rows info 秀在頂端
       DataViewForm.setPageBtnOnButtom(true);      //將button and rows info 秀在底端
       DataViewForm.setFilterShow(false);           //是否要秀出過濾選擇欄位
       DataViewForm.setJumpPageShow(false);         //是否要秀出跳頁下拉式選單
       DataViewForm.setRowsInfoShow(false);         //是否要秀出筆數資訊
       DataViewForm.setRowPageShow(false);          //是否要秀出每頁筆數控制欄
       DataViewForm.setExcelShow(false);           //是否要秀出轉Excel button
       DataViewForm.setOrderEnable(false);          //是否要提供排序點選功能
       DataViewForm.setShowHeaderWhenNoData(false);//當ResultSet無資料是否要秀Header

       DataViewForm.setTableHeaderHtml("class='td_head_grid' align='center'  width='680px'");
             //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
       DataViewForm.setPageActionURI(stbPageActionURI.toString());
   }

   String getFrameSysNo(View view) throws Exception{
      // 取得IN 字串
      String strSysData="";
      String[] strFrame;

      if (view.getsys_group()!=null&&view.getsys_group().length()>0){
         strFrame=view.getsys_group().split(",");

         if (strFrame.length<1){
            strSysData="''";
         }

         for(int i=0;i<strFrame.length;i++){
            strSysData +="'" + strFrame[i].trim() + "'";
            if (i<strFrame.length -1){
               strSysData += ",";
            }
         }
      } else {
         strSysData="''";
      }

      return strSysData;
   }

   boolean exeUpdSQL(View view,int pintSn){
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{

         lvtrParam.add(view.getseqno()==null ? null : view.getseqno()[pintSn]);
         lvtrParam.add(view.getsys_no()==null ? null : view.getsys_no()[pintSn]);
         lvtrParam.add(view.gettype_id()==null ? null : view.gettype_id()[pintSn]);
         lvtrParam.add(view.gettype_up()==null ? null : view.gettype_up()[pintSn]);

         lstrSQL="update wkf_menu set seqno=? where sys_no=? and type_id=? and type_up=? ";
         return gqbnDAO.executeSQL(lstrSQL,lvtrParam);
      }
      catch(Exception e){
         e.printStackTrace();
         return false;
      }
      finally{
         lvtrParam.clear();
      }
   }


   String batchUpdateData(View view) throws Exception{
       String[] lstrRowSn=view.getrowsn().split(",");

       try{
           for(int i=0;i<lstrRowSn.length;i++){
               if(!exeUpdSQL(view,Integer.parseInt(lstrRowSn[i]))){
                   throw new Exception("update error");
               }
           }
           return "儲存目錄排序成功!";
       }
       catch(Exception e){
           return "儲存目錄排序失敗!!";
       }
   }

   String checkCanModifyFrameNo(View view) throws Exception{
       // 檢查是否可以修改Frame No
       String strQty="0";
       strQty = (gqbnDAO.readSingleValueData("select count(frame_no) from wkf_frame_main " +
                                             " where trim(upper(frame_no))='" + view.getframe_no().trim().toUpperCase() + "'" +
                                             "   and use_yn='Y'"));
       return strQty;
   }

   String checkHaveFrameNo(View view) throws Exception{
       // 檢查Frame No是否已存在
       String strQty="0";
       strQty = (gqbnDAO.readSingleValueData("select count(frame_no) from wkf_frame " +
                                             " where trim(upper(frame_no))='" + view.getframe_no().trim().toUpperCase() + "'"));
       return strQty;
   }

   void saveDataToWkf_Sys_Table(String strFrameNo) throws Exception{
      // 檢查資料庫中是否已經有資料,沒有則新增資料至表格中
      String strQty="0";
       strQty = (gqbnDAO.readSingleValueData("select count(frame_no) from wkf_frame_main " +
                                             " where trim(upper(frame_no))='" + strFrameNo.toUpperCase() + "'"));
      if (strQty.equals("0")){
         exeInsWkfFrameMainSQL(strFrameNo);
      }
   }

   String exeInsWkfFrameMainSQL(String strFrameNo) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lvtrParam.add(strFrameNo);
         lvtrParam.add("N");
         lstrSQL="insert into wkf_frame_main(frame_no,use_yn) values (?,?)";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){;
            return "wkf_frame_main";
         }
         lvtrParam.clear();

         return "";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e);
      }
      finally{
         lvtrParam.clear();
      }
   }



   String checkNotNullFld(View view) throws Exception{
      StringBuffer lstbNullFldList=new StringBuffer();
      return (lstbNullFldList.toString().equals("") ? "" : lstbNullFldList.delete(lstbNullFldList.length()-1,lstbNullFldList.length()).toString());
   }
   void setInitValueForAdd(View view,String pstrUserID) throws Exception{
   }
   void setReadonlyForAdd(View view) throws Exception{
   }

}
